﻿using System;
using System.IO;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
using System.Security.Principal;
using System.Threading;


namespace shfa.snomtastic.systray.forms
{
    public partial class frmLogin : Form
    {
        private string userSID;

        #region PublicMembers

        public void SetDisplay(string headerText, string messageText, string userName, string userSid)
        {
            gbMessage.Text = headerText;
            lblPhoneMessage.Text = messageText;
            txtUserName.Text = userName;
            userSID = userSid;
        }

        #endregion



        #region constructor

        public frmLogin()
        {
            InitializeComponent();
            txtPassword.Focus();
        }

        #endregion


        #region Events
       
        private void btnLogin_Click(object sender, EventArgs e)
        {
            UserService.user RemoteUserService = new ClientUserService();
            try
            {
                bool isValid = RemoteUserService.isValidCredentials(txtUserName.Text, txtPassword.Text);
                if (isValid)
                {
                    bool bUpdated = RemoteUserService.UpdatePassword(userSID, txtPassword.Text);
                    if (bUpdated)
                    {
                        MessageBox.Show("Password has been updated. Your phone will now reboot.", "Password Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Password update failed.", "Password Update", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("Password is incorrect.", "Password Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                txtPassword.Text = "";
                btnLogin.Enabled = false;
            }
            catch
            {
                MessageBox.Show("Could not Connect to server.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            RemoteUserService.Dispose();
            RemoteUserService = null;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            if (txtPassword.Text != string.Empty)
            {
                btnLogin.Enabled = true;
            }
        }

        #endregion


        
       

       

    }
}
